package pl.diagnoser.server.lpr;

import java.io.InputStream;
import java.util.List;
import lprlibrary.io.SimpleFileDataManager;
import pl.diagnoser.client.dto.DefectDictDTO;
import pl.diagnoser.client.dto.DiagnoserResultDTO;
import pl.diagnoser.client.dto.ObjectsDTO;
import pl.diagnoser.client.dto.SymptomDTO;
import pl.diagnoser.client.tools.SearchObjectAttribute;

public class DiagnoserCore {
	
	private static DiagnoserCore instance = null;
	private static InputStream knowledgeBaseFileStream = null;
	private DiagnoserKnowledgeBase knowledgeBase;
	private SimpleFileDataManager dataLoader;
	private DiagnoserEngine engine;
	
	public static DiagnoserCore getInstance(InputStream knowledgeBaseFile) {
		if (instance == null) {
			instance = new DiagnoserCore();
		}
		knowledgeBaseFileStream = knowledgeBaseFile;
		return instance;
	}
	
	public List<DefectDictDTO> getPossibleDefects(ObjectsDTO object, List<SymptomDTO> symptoms) {
        knowledgeBase = new DiagnoserKnowledgeBase();
        dataLoader = new SimpleFileDataManager(knowledgeBase);
        dataLoader.setSourceFile(knowledgeBaseFileStream);
        dataLoader.load();
        engine = new DiagnoserEngine(knowledgeBase);
        List<DefectDictDTO> defects = null;
        if (object != null && symptoms != null) {
                defects = engine.getPossibleDefects(object, symptoms);
        }
        return defects;
	}

	public List<DiagnoserResultDTO> getPossibleDefects(List<SearchObjectAttribute> searchCriteria, List<SearchObjectAttribute> emptySearchCriteria, boolean fast) {
		knowledgeBase = new DiagnoserKnowledgeBase();
		dataLoader = new SimpleFileDataManager(knowledgeBase);
		dataLoader.setSourceFile(knowledgeBaseFileStream);
		dataLoader.load();
		engine = new DiagnoserEngine(knowledgeBase);
		List<DiagnoserResultDTO> defects = null;
		if ( searchCriteria != null || emptySearchCriteria != null) {
			defects = engine.getPossibleDefects(searchCriteria, emptySearchCriteria, fast);
		}
		return defects;
	}
	
	public DiagnoserKnowledgeBase getKnowledgeBase() {
		return knowledgeBase;
	}
}
